
* replicates Table A3 in the online appendix

clear *

* program to calculate marginals 
capture program drop marginals
program define marginals
	args x /* x = matrix name */
	mat `x' = r(table)'
	local r = rowsof(`x')
	mat `x' = `x'[1..`r',1..1]
end

/*-------------------------------------------------------------------------------*/
/* Our YouGov survey */
/*-------------------------------------------------------------------------------*/

use survey, clear

keep if incomeall >= 14

* ideology and PID
gen dem = (pid7 == 1 | pid7 == 2) if pid7 ~= . 
gen rep = (pid7 == 6 | pid7 == 7) if pid7 ~= .
gen lib = (ideo5 == 1 | ideo5 == 2) if ideo5 ~= .
gen cons = (ideo5 == 4 | ideo5 == 5) if ideo5 ~= .
mean dem rep lib cons 
marginals yg1

* attitudes
recode trust (1/2 = 0) (3/5 = 1), gen(distrust)
foreach x in health school jobs {
	recode govt_`x' (1/2 = 1) (3/7 = 0), gen(`x')
	}
recode inc_equal (2/7 = 0), gen(ineq1)
mean distrust health school jobs ineq1 
marginals yg2

/*-------------------------------------------------------------------------------*/
/* 2016 ANES */
/*-------------------------------------------------------------------------------*/

use nes2016, clear
* keep high-income respondents
replace V161361x = 28 if V161361x ~= 28 & V162309x == 28
keep if V161361x == 28
* keep relevant variables only
keep V162171 V161158x V161215 V162193 V161206 V161189 V162148

* ideology and PID
rename V162171 ideo7
rename V161158x pid7
gen dem = (pid7 == 1 | pid7 == 2) if pid7 ~= . 
gen rep = (pid7 == 6 | pid7 == 7) if pid7 ~= .
recode ideo7 (-7/-6 = .) (99 = .)
gen lib = (ideo7 == 1 | ideo7 == 2) if ideo7 ~= .
gen cons = (ideo7 == 5 | ideo7 == 6 | ideo7 == 7) if ideo7 ~= .
mean dem rep lib cons 
marginals nes1 

* attitudes
recode V161215 (1/2 = 0) (3/5 = 1), gen(distrust)
recode V162193 (-9/-6 = .)
gen health = (V162193 == 2) if V162193 ~= .
gen school = (V161206 == 2) 
recode V161189 (99 = .) (6/7 = 1) (1/5 = 0), gen(jobs)
recode V162148 (-7/-6 = .)
gen ineq = (V162148 == 2) if V162148 ~= .
mean distrust health school jobs ineq 
marginals nes2

/*-------------------------------------------------------------------------------*/
/* 2016 CCES (Common core) */
/*-------------------------------------------------------------------------------*/

use cces2016, clear
* keep high-income respondents
recode faminc (97 = .)
keep if faminc == 14 | faminc == 15 | faminc == 16
* keep relevant variables only
keep pid7 ideo5 CC16_426_2 CC16_426_3

* ideology and PID
gen dem = (pid7 == 1 | pid7 == 2) if pid7 ~= . 
gen rep = (pid7 == 6 | pid7 == 7) if pid7 ~= .
gen lib = (ideo5 == 1 | ideo5 == 2) if ideo5 ~= .
gen cons = (ideo5 == 4 | ideo5 == 5) if ideo5 ~= .
mean dem rep lib cons 
marginals cces1

* attitudes
recode CC16_426_2 (1/3 = 0) (4/5 = 1), gen(health)
recode CC16_426_3 (1/3 = 0) (4/5 = 1), gen(school)
mat cces2 = J(5,1,.)
mean health
mat tab = r(table)
mat cces2[2,1] = tab[1,1]
mean school
mat tab = r(table)
mat cces2[3,1] = tab[1,1]

/*-------------------------------------------------------------------------------*/
/* Comparison table */
/*-------------------------------------------------------------------------------*/

mat head1 = J(1,3,.)
mat head2 = J(1,3,.)
mat ideo = yg1,nes1,cces1
mat pol = yg2,nes2,cces2
mat comp = head1 \ ideo \ head2 \ pol
local c = colsof(comp)
local r = rowsof(comp)
forval i = 1/`r' {
	forval j = 1/`c' {
		mat comp[`i',`j'] = comp[`i',`j']*100
		}
	}
frmttable, clear
frmttable, statmat(comp) substat(0) sdec(1) ///
	rtitles("\textbf{Ideology \& PID}" \ "\hspace{1em} Democrat" \ ///
			"\hspace{1em} Republican" \ "\hspace{1em} Liberal" \ ///
			"\hspace{1em} Conservative" \ "\textbf{Political attitudes}" \ ///
			"\hspace{1em} Low trust in gov't" \ "\hspace{1em} Smaller role of gov't---health care" \ ///
			"\hspace{1em} Smaller role of gov't---education" \ ///
			"\hspace{1em} Smaller role of gov't---jobs" \ "\hspace{1em} Inequality tolerance")
